Ext.namespace('GBIF.GeoSpatial')		

GBIF.GeoSpatial.WizardPanel = function(config){
	
	this.type = new GBIF.GeoSpatial.LayerTypes({
			itemId:'combo'
		,	title: 'Step 1 of 3'
	})

	this.georss = new GBIF.GeoSpatial.Georss({
			itemId:'georss'
		,	title: 'Step 2 of 3'
	})

	this.WMS = new GBIF.GeoSpatial.WMSCapabilities({
			itemId:'wms'
		,	title: 'Step 2 of 3'
	})
	
	this.wfscapabilities = new GBIF.GeoSpatial.WFSCapabilities({
			itemId:'wfs'
		,	title:	'Step 2 of 3'
	});

	this.info = new GBIF.GeoSpatial.Information({
			itemId:'info'
		,	title: 'Step 3 of 3'
	})
	this.summary = new GBIF.GeoSpatial.Summary({
			title:'Summary'
		,	id:'summary'
	})
	this.data={};
	
	Ext.apply(this,config,{
	    	title: 'Add Layer Wizard'
		,	autoWidth: true
		,	height:500
	    ,	layout:'card'
	    ,	activeItem: 0
	    ,	border:true
	    ,	tbar: [
			     {
			            ref: '../moveprev'
			        ,	text: 'Back'
					,	scope:this
			        ,   disabled: true
					,	handler:this.previous
			     },
			     {
			            ref: '../movenext'
			        ,   text: 'Next'
					,	style:"padding:0px 5px"
					,	scope:this
			        ,   handler: this.next 
			     }
			    ]
	    ,	items: [this.type,this.georss,this.info, this.WMS, this.summary, this.wfscapabilities]
	});
	
	GBIF.GeoSpatial.WizardPanel.superclass.constructor.call(this, config)
}
Ext.extend(GBIF.GeoSpatial.WizardPanel,Ext.Panel, {
		
		next:function(){

			switch(this.layout.activeItem.itemId){
				
				case 'combo':
				
					if(this.type.getForm().getValues().typeCombo=='Select Layer'){
						GBIF.GeoSpatial.Notice.msg("Error","Please select Layer Type");
					}
					else{
						this.data.mtype=this.type.getForm().getValues().typeCombo;
						if(this.type.getForm().getValues().typeCombo=='WMS'){
							this.layout.setActiveItem('wms');
							this.movenext.setText('Finish');
							this.WMS.url.focus();
						}else if(this.type.getForm().getValues().typeCombo=='WFS'){
							this.layout.setActiveItem('wfs');
							this.wfscapabilities.url.focus();
							this.movenext.setText('Finish');
						}else if(this.type.getForm().getValues().typeCombo=='GBIF Checklist Bank'){
							this.layout.setActiveItem('info');
							this.info.getComponent('layerId').focus();
							this.movenext.setText('Finish');
						}else {
							this.layout.setActiveItem('georss');
							this.georss.url.focus();
						}
						this.moveprev.enable();
					}
					break;
					
				case 'georss':
					if (this.type.getForm().getValues().typeCombo == 'KML') {
						this.data.url = this.georss.url.getValue();
						this.layout.setActiveItem('info');
						this.movenext.setText('Finish');
					}else {
						if (this.georss.url.getValue() == '') {
							GBIF.GeoSpatial.Notice.msg("error", "Please enter URL");
						}else {
							this.data.url = this.georss.url.getValue();
							this.layout.setActiveItem('info');
							this.movenext.setText('Finish');
						}
					}
					break;
					
				case 'wms':

					if (this.WMS.url.getValue() != '') {
						this.data.url=this.WMS.url.getValue();
						this.layout.setActiveItem('combo');
						this.movenext.setText('Next');
						this.moveprev.disable();
						this.reset();
					}else
						GBIF.GeoSpatial.Notice.msg("error","Please enter URL");
					break;	
				
				case 'wfs':
					
					if (this.wfscapabilities.url.getValue() != '') {
						this.data.url=this.wfscapabilities.url.getValue();
						this.layout.setActiveItem('combo');
						this.movenext.setText('Next');
						this.moveprev.disable();
						this.reset();
					}else
						GBIF.GeoSpatial.Notice.msg("error","Please enter URL");
					break;
						
				case 'info':
					if(this.info.getForm().getValues().infoName==''){
							GBIF.GeoSpatial.Notice.msg("error","Please Enter Name");
					}else{
						this.data.name=this.info.getForm().getValues().infoName;
						this.data.description=this.info.getForm().getValues().description;
						this.fireEvent("addLayer", this.data ); 
						this.reset();
						this.layout.setActiveItem('combo');
						this.movenext.setText('Next');
						this.moveprev.disable();
					}
			}
		}
			
	,	previous: function(){
			
			switch(this.layout.activeItem.itemId){
				case 'georss':
					this.layout.setActiveItem('combo');
					this.movenext.setText('Next');
					this.moveprev.disable();
					break;
				case 'wms':
					this.layout.setActiveItem('combo');
					this.movenext.setText('Next');
					this.moveprev.disable();
					
					break;	
				case 'wfs':
					this.layout.setActiveItem('combo');
					this.movenext.setText('Next');
					this.moveprev.disable();
					break;
					
				case 'info':
				
					/*
if(this.type.getForm().getValues().typeCombo=='WMS'){
						this.layout.setActiveItem('wms');
					}else if(this.type.getForm().getValues().typeCombo=='WFS'){
						this.layout.setActiveItem('wfs');
					}else 
*/
					if(this.type.getForm().getValues().typeCombo=='GBIF Checklist Bank'){
						this.layout.setActiveItem('combo');
					}else{
						this.layout.setActiveItem('georss');
					} 
					this.movenext.setText('Next');
			}
		}
	,	reset: function(){
			this.type.getComponent('layerType').setValue('Select Layer');
			this.georss.url.setValue('');
			this.WMS.url.setValue('');
			this.WMS.clearGrid();
			this.wfscapabilities.url.setValue('');
			this.info.getComponent('layerId').setValue('');
			this.info.getComponent('descId').setValue('');
			this.data={};
		}
})